package com.hnucm.c201901020241.my;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hnucm.c201901020241.R;
import com.hnucm.c201901020241.pojo.User;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class Update_password extends AppCompatActivity {
    String name,ac,psw,base64,signature,sex,birthday,grade,college;
    TextView numChage;
    EditText old,New;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_password);
        old=findViewById(R.id.textView61);
        numChage=findViewById(R.id.textView62);
        New=findViewById(R.id.textView65);
        SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_MULTI_PROCESS);
        ac= sharedPreferences.getString("account","");
        psw= sharedPreferences.getString("password","");
        name= sharedPreferences.getString("name", "null");
        base64= sharedPreferences.getString("base64","");
        signature= sharedPreferences.getString("signature","");
        sex= sharedPreferences.getString("sex","");
        birthday= sharedPreferences.getString("birthday","");
        grade= sharedPreferences.getString("grade","");
        college= sharedPreferences.getString("college","");
        findViewById(R.id.imageView19).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.textView64).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password_new=New.getText().toString();
                if(password_new==null){
                    Toast.makeText(Update_password.this,"密码不能为空",Toast.LENGTH_SHORT).show();
                }
                else{
                    if(old.getText().toString().equals(psw)) {
                        User user = new User(ac, password_new, name,signature, sex, birthday, grade, college,base64);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("password", password_new);
                        psw=password_new;
                        editor.commit();
                        x.Ext.init(getApplication());
                        RequestParams requestParams = new RequestParams("http://121.4.27.254:8090/updateUser?");
                        requestParams.addQueryStringParameter("account", ac);
                        requestParams.addQueryStringParameter("password", password_new);
                        requestParams.addQueryStringParameter("name", name);
//                        requestParams.addQueryStringParameter("base64", base64);
                        requestParams.addQueryStringParameter("signature", signature);
                        requestParams.addQueryStringParameter("sex", sex);
                        requestParams.addQueryStringParameter("birthday", birthday);
                        requestParams.addQueryStringParameter("grade", grade);
                        requestParams.addQueryStringParameter("college", college);
                        x.http().post(requestParams, new Callback.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String result) {
                                if (result.equals("1"))
                                    Toast.makeText(Update_password.this, "修改成功", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {
                                Toast.makeText(Update_password.this, "网络出现异常错误，请稍后", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onCancelled(CancelledException cex) {
                            }

                            @Override
                            public void onFinished() {

                            }
                        });
                    }
                    else{
                        Toast.makeText(Update_password.this, "原密码不正确，请重新输入", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}